<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <script>
        //做一个二分查询
        // 输入：已经排好序的数组arr，一个待查找的值data
        // 输出 true,false/返回索引值，-1
        function binarySearch(arr, data) {
            //1.设置指针
            let count = 0
            let lower = 0;
            let upper = arr.length - 1;
            while (lower <= upper) {
                let mid = Math.floor((lower + upper) / 2);
                if (arr[mid] < data) {
                    lower = mid + 1;
                    count++
                } else if (arr[mid] > data) {
                    upper = mid - 1;
                    count++
                } else {
                    console.log(mid);
                    console.log(count);
                }
            }
            console.log(-1)
            console.log(count);
        }
        const arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]
        console.log('二分查找', binarySearch(arr, 7));
    </script>
</body>

</html>